package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.InsSchemeForm;
import com.yxinsur.imc.business.vo.InsSchemeVo;
import com.yxinsur.imc.business.vo.select.SelectInsScheme;
import com.yxinsur.imc.common.exception.BusinessException;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxinsur.imc.business.mapper.ImcInsSchemeMapper;
import com.yxinsur.imc.business.domain.ImcInsScheme;
import com.yxinsur.imc.business.service.IImcInsSchemeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 险种方案
每个租户可以为自己的险种配置险种方案Service业务层处理
 * 
 * @author imc
 * @date 2021-04-15
 */
@Service
public class ImcInsSchemeServiceImpl extends ServiceImpl<ImcInsSchemeMapper, ImcInsScheme> implements IImcInsSchemeService
{
    private static final Logger logger = LoggerFactory.getLogger(ImcInsSchemeServiceImpl.class);

    @Autowired
    private ImcInsSchemeMapper imcInsSchemeMapper;

    /**
     * 查询险种方案
     *
     * @param insSchemeForm 险种方案
     * @return 险种方案
     */
    @Override
    public List<InsSchemeVo> selectImcInsSchemeList(InsSchemeForm insSchemeForm)
    {
        return imcInsSchemeMapper.selectImcInsSchemeList(insSchemeForm);
    }

    /**
     * 新增险种方案
     *
     * @param insSchemeForm 险种方案
     * @return 结果
     */
    @Override
    @Transactional
    public int insertImcInsScheme(InsSchemeForm insSchemeForm) {
        logger.info("险种方案新增操作：{}",insSchemeForm.toString());
        QueryWrapper<ImcInsScheme> schemeQueryWrapper = new QueryWrapper<>();
        schemeQueryWrapper.eq("repo_ins_id",insSchemeForm.getRepoInsId());
        schemeQueryWrapper.eq("ins_scheme_value",insSchemeForm.getInsSchemeValue());
        schemeQueryWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
        schemeQueryWrapper.eq("tenant_id",SecurityUtils.getTenantId());
        int count = this.imcInsSchemeMapper.selectCount(schemeQueryWrapper);
        if(count > 0){
            throw new BusinessException("新增的险种方案名["+insSchemeForm.getInsSchemeValue()+"]已存在，请更换方案名");
        }else{
            ImcInsScheme scheme = new ImcInsScheme();
            BeanUtils.copyProperties(insSchemeForm,scheme);
            scheme.setId(null);
            scheme.setCreateTime(DateUtils.getNowDate());
            scheme.setCreateId(SecurityUtils.getUsername());
            scheme.setIsDeleted(IsDeletedEnum.N.getCode());
            scheme.setTenantId(SecurityUtils.getTenantId());
            return this.imcInsSchemeMapper.insert(scheme);
        }
    }

    /**
     * 修改险种方案
     *
     * @param insSchemeForm 险种方案
     * @return 结果
     */
    @Override
    public int updateImcInsScheme(InsSchemeForm insSchemeForm) {
        logger.info("险种方案更新操作：{}",insSchemeForm.toString());
        QueryWrapper<ImcInsScheme> schemeQueryWrapper = new QueryWrapper<>();
        schemeQueryWrapper.ne("id",insSchemeForm.getId());
        schemeQueryWrapper.eq("repo_ins_id",insSchemeForm.getRepoInsId());
        schemeQueryWrapper.eq("ins_scheme_value",insSchemeForm.getInsSchemeValue());
        schemeQueryWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
        schemeQueryWrapper.eq("tenant_id",SecurityUtils.getTenantId());
        int count = this.imcInsSchemeMapper.selectCount(schemeQueryWrapper);
        if(count > 0){
            throw new BusinessException("更新的险种方案名["+insSchemeForm.getInsSchemeValue()+"]已存在，请更换方案名");
        }else{
            ImcInsScheme scheme = this.imcInsSchemeMapper.selectById(insSchemeForm.getId());
            BeanUtils.copyProperties(insSchemeForm,scheme);
            scheme.setUpdateTime(DateUtils.getNowDate());
            scheme.setUpdateId(SecurityUtils.getUsername());
            return this.imcInsSchemeMapper.updateById(scheme);
        }
    }

    /**
     * 批量删除险种方案
     *
     * @param ids 方案id数组
     * @return 结果
     */
    @Override
    public int deleteInsSchemeByIds(Long[] ids) {
        logger.info("险种方案删除操作：{}",ids);
        int count = 0;
        for(Long id:ids){
            ImcInsScheme scheme = this.imcInsSchemeMapper.selectById(id);
            scheme.setIsDeleted(scheme.getId().toString());
            scheme.setUpdateId(SecurityUtils.getUsername());
            scheme.setUpdateTime(DateUtils.getNowDate());
            count += this.imcInsSchemeMapper.updateById(scheme);
        }
        return count;
    }

    /**
     * 下拉列表数据获取
     *
     * @param repoInsId 产品id
     * @return 结果
     */
    @Override
    public List<SelectInsScheme> selectDataList(Long repoInsId) {
        QueryWrapper<ImcInsScheme> schemeQueryWrapper = new QueryWrapper<>();
        schemeQueryWrapper.eq("repo_ins_id",repoInsId);
        schemeQueryWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
        schemeQueryWrapper.eq("tenant_id",SecurityUtils.getTenantId());
        List<ImcInsScheme> list = this.imcInsSchemeMapper.selectList(schemeQueryWrapper);
        List<SelectInsScheme> selectInsSchemes = new ArrayList<>();
        list.parallelStream().forEach(
                insScheme -> {
                    SelectInsScheme scheme = new SelectInsScheme();
                    BeanUtils.copyProperties(insScheme,scheme);
                    selectInsSchemes.add(scheme);
                }
        );

        return selectInsSchemes;
    }
}
